<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Order\Pay;
use App\Admin\Actions\Order\Ship;
use App\Admin\Tools;
use App\Models\Customer;
use App\Models\Order;
use Encore\Admin\Controllers\AdminController as Controller;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Displayers\DropdownActions;
use Encore\Admin\Show;

class OrderController extends Controller
{
    protected $title = "订单管理";

    protected function grid()
    {
        $grid = new Grid(new Order);

        $grid->setActionClass(DropdownActions::class);
        // 搜索
        $grid->filter(function ($filter) {
            // 去掉默认的id
            $filter->disableIdFilter();
            $filter->like('order_number', '订单号');
            $filter->like('customer.username', '顾客名称');
            $filter->where(function ($q) {
                $q->status($this->input);
            }, '订单状态')->select([
                "pay" => "待付款",
                "ship" => "待收货",
                "comment" => "待评价",
                "cancel" => "已取消",
                "refund" => "退换货",
            ]);
        });

        // 屏蔽操作
        $grid->disableCreateButton();
        $grid->disableRowSelector();

        // 操作栏
        $grid->actions(function ($actions) use ($grid) {
            // 去掉删除
            $actions->disableDelete();

            // 去掉编辑
            $actions->disableEdit();

            // 添加发货按钮
            $actions->add(new Ship());

            // 添加线下转账
            $actions->add(new Pay());
        });

        // 默认排序
        $grid->model()->orderByDesc('id');

        $grid->columns([
            'id' => '订单ID',
            'order_number' => '订单号',
            'payment_no' => '支付流水号',
            'customer.username' => '顾客名称',
            'total_account' => '总价',
            'total_amount' => '总数',
            'paid_at' => '支付时间',
            'created_at' => '提交时间',
            'remark' => '备注',
        ]);

        $grid->column('payment_method', '支付方式')->using(Order::$payment);

        $grid->column('supplement_payment_money', '钱包支付金额')->display(function ($r) {
            return price($r / 100);
        });

        $grid->column('payment_money', '其他支付金额')->display(function ($r) {
            return price($r / 100);
        });

        $grid->column('status', '订单状态')->display(function () {
            return $this->status;
        });

        $grid->column('status_comment', '状态说明')->display(function () {
            return $this->status_comment;
        });

        return $grid;
    }

    protected function detail($id)
    {
        $show = new Show(Order::withTrashed()->findOrFail($id));

        $show->field('id', '订单ID');
        $show->field('order_number', '订单号');
        $show->field('status', '订单状态');

        $show->field('deleted_at', '是否删除')->as(function ($v) {
            return $v ? '已删除' : '正常';
        });

        $show->field('closed', '是否关闭')->as(function ($v) {
            return $v ? '已关闭' : '正常';
        });

        $show->field('customer_id', '顾客信息')->as(function ($id) {
            $customer = Customer::find($id);
            return [
                "ID: " . $customer->id,
                "名称: " . $customer->username,
            ];
        })->label();

        $show->field('address', '收货信息')->as(function ($address) {
            return [
                $address['province'],
                $address['city'],
                $address['district'],
                $address['address'],
                "联系人: " . $address['contact_name'],
                "联系电话: " . $address['contact_phone'],
            ];
        })->label();
        $show->field('total_account', '订单总金额');
        $show->field('total_amount', '商品总数量');
        $show->field('remark', '备注');
        $show->field('paid_at', '支付时间');
        $show->field('payment_method', '支付方式')->using(Order::$payment);
        $show->field('payment_no', '支付订单号');

        $show->items('商品列表', function (Grid $items) {
            $items->column('product_id', '商品ID');
            $items->column('product_sku_id', '商品SKU');
            $items->column('detail', '商品名称')->display(function ($detail) {
                return $detail['name'] . '-'
                    . json_encode($detail['attribute'], JSON_UNESCAPED_UNICODE);
            });
            $items->column('amount', '数量');
            $items->column('price', '价格');
            $items->column('rating', '评分');

            $items->disableCreateButton();
            $items->disableFilter();
            $items->disableExport();
            $items->disableRowSelector();
            $items->disableActions();
            $items->disableColumnSelector();
            $items->disablePagination();

        });

        $show->panel()->tools(function ($tools) {
            $tools->disableEdit();
            $tools->disableDelete();
        });

        return $show;
    }

    protected function form()
    {
        $form = new Form(new Order);
        $form->text('order_number', '订单号');
        $form->number('customer_id', 'Customer id');
        $form->text('address', 'Address');
        $form->decimal('total_account', 'Total account');
        $form->text('remark', 'Remark');
        $form->datetime('paid_at', 'Paid at')->default(date('Y-m-d H:i:s'));
        $form->switch('payment_method', 'Payment method');
        $form->text('payment_no', 'Payment no');
        $form->text('refund_status', 'Refund status')->default('pending');
        $form->text('refund_no', 'Refund no');
        $form->switch('closed', 'Closed');
        $form->switch('reviewed', 'Reviewed');
        $form->text('ship_status', 'Ship status')->default('pending');
        $form->text('ship_data', 'Ship data');
        $form->text('extra', 'Extra');

        return $form;
    }

    protected function showAddress($address, $fields)
    {
        $str = "";

        foreach ($fields as $field => $v) {
            $str .= $v . ":" . $address[$fields] . PHP_EOL;
        }

        return $str;
    }
}
